import Vue from 'vue'
import Vuex from 'vuex'
import { upload, uploadList } from '@/api/oa/index.js'
import { Message } from 'element-ui'
Vue.use(Vuex)
export default new Vuex.Store({
  state: {
    token: '',
    files: [],
    uploadArr: []
  },
  mutations: {
    changeFile (state, list) {
      if (list.length !== 0) {
        state.files = list
      } else {
        state.files = []
      }
    },
    getUpload (state, data) {
      if (data.length !== 0) {
        state.uploadArr.push(data)
      } else {
        state.uploadArr = []
      }
      console.log('this.$store.state.uploadArr', data)
    }
  },
  actions: {
    filesDo (context) {
      const approve = JSON.parse(window.localStorage.getItem('approve'))
      const params = {
        workflow_id: approve.workflow_id
      }
      uploadList(params).then(res => {
        const { status, data } = res
        if (status) {
          // this.files = data
          context.commit('changeFile', data)
        }
      })
    },
    uploadDo (context, fd) {
      return new Promise((resolve, reject) => {
        upload(fd).then(res => {
          const { status, data } = res
          if (status) {
            Message.error('上传成功')
            context.commit('getUpload', data)
            resolve(res)
          } else {
            reject(res)
          }
        })
      })
    }
  },
  modules: {
  }
})
